home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_001 / trackdisk / trackdisk.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  273 lines

  1. /* This program is probably copyright Commodore-Amiga.  It was mailed
  2.  * to me directly by Robert Peck at Commodore-Amiga in response
  3.  * to a question about a previous version's copyright status.  I
  4.  * therefore assume that it is Commodore-Amiga's intention that this
  5.  * program be freely redistributable.  Fred Fish, 3-Jan-86.
  6.  */
  7.  
  8. /* trackdisk.c */
  9.  
  10. /* program to demonstrate the use of the trackdisk driver */
  11.  
  12. /* author: Rob Peck, 12/1/85 */
  13.  
  14. /* This code may be freely utilized to develop programs for the Amiga */
  15.  
  16.  
  17. #include "exec/types.h"
  18. #include "exec/nodes.h"
  19. #include "exec/lists.h"
  20. #include "exec/memory.h"
  21. #include "exec/interrupts.h"
  22. #include "exec/ports.h"
  23. #include "exec/libraries.h"
  24. #include "exec/io.h"
  25. #include "exec/tasks.h"
  26. #include "exec/execbase.h"
  27. #include "exec/devices.h"
  28. #include "devices/trackdisk.h"
  29.  
  30. #define TD_READ CMD_READ
  31. #define BLOCKSIZE TD_SECTOR
  32.  
  33. SHORT error;
  34. struct MsgPort *diskport;
  35. struct IOExtTD *diskreq;
  36. BYTE diskbuffer[BLOCKSIZE];
  37. BYTE *diskdata;
  38. SHORT testval;
  39.  
  40. extern struct MsgPort *CreatePort();
  41. extern struct IORequest *CreateExtIO();
  42.  
  43. ULONG diskChangeCount;
  44.          
  45. ReadCylSec(cyl, sec, hd)
  46. SHORT cyl, sec, hd;
  47. {
  48.         LONG offset;
  49.  
  50.         diskreq->iotd_Req.io_Length = BLOCKSIZE;
  51.         diskreq->iotd_Req.io_Data = (APTR)diskbuffer;
  52.                 /* show where to put the data when read */
  53.         diskreq->iotd_Req.io_Command = ETD_READ;
  54.                 /* check that disk not changed before reading */
  55.         diskreq->iotd_Count = diskChangeCount;
  56.         
  57.         /* convert from cylinder, head, sector to byte-offset value to get
  58.          * right one (as dos and everyone else sees it)...*/
  59.          
  60.         /* driver reads one CYLINDER at a time (head does not move for
  61.          * 22 sequential sector reads, or better-put, head doesnt move for
  62.          * 2 sequential full track reads.)
  63.          */
  64.         
  65.         offset = TD_SECTOR * (sec + NUMSECS * hd + NUMSECS * NUMHEADS * cyl);
  66.         diskreq->iotd_Req.io_Offset = offset;
  67.         DoIO(diskreq);
  68.         return(0);
  69. }
  70.   
  71. MotorOn()
  72. {
  73.         /* TURN ON DISK MOTOR ... old motor state is returned in io_Actual */
  74.         diskreq->iotd_Req.io_Length = 1;
  75.         /* this says motor is to be turned on */
  76.         diskreq->iotd_Req.io_Command = TD_MOTOR;
  77.         /* do something with the motor */
  78.         DoIO(diskreq);
  79.         printf("\nOld motor state was: %ld",diskreq->iotd_Req.io_Actual);
  80.         printf("\nio_Error value was: %ld",diskreq->iotd_Req.io_Error);
  81.         return(0);
  82. }
  83.  
  84. MotorOff()
  85. {
  86.         printf("\n\nNow turn it off");
  87.         diskreq->iotd_Req.io_Length = 0;
  88.         /* says that motor is to be turned on */
  89.         diskreq->iotd_Req.io_Command = TD_MOTOR;
  90.         /* do something with the motor */
  91.         DoIO(diskreq);
  92.         printf("\nOld motor state was: %ld",diskreq->iotd_Req.io_Actual);
  93.         printf("\nio_Error value was: %ld",diskreq->iotd_Req.io_Error);
  94.         return(0);
  95. }
  96.   
  97. SeekFullRange(howmany)
  98. SHORT howmany;
  99. {
  100.         int i;
  101.         for(i=0; i<howmany; i++)
  102.         {
  103.                 diskreq->iotd_Req.io_Offset =
  104.                         ((NUMCYLS -1)*NUMSECS*NUMHEADS -1 ) * 512;
  105.                 /* seek to cylinder 79, head 1 */
  106.                 diskreq->iotd_Req.io_Command = TD_SEEK;
  107.                 DoIO(diskreq);
  108.                 if(diskreq->iotd_Req.io_Error != 0)
  109.                         printf("\nSeek Cycle Number %ld, Error = %ld",
  110.                                                 i, diskreq->iotd_Req.io_Error);
  111.                 diskreq->iotd_Req.io_Offset = 0;
  112.                         /* seek to cylinder 0, head 0 */
  113.                 diskreq->iotd_Req.io_Command = TD_SEEK;
  114.                 DoIO(diskreq);
  115.                 if(diskreq->iotd_Req.io_Error != 0)
  116.                         printf("\nSeek Cycle Number %ld, Error = %ld",
  117.                                                 i, diskreq->iotd_Req.io_Error);
  118.                 printf("\nCompleted a seek");
  119.         }
  120.         return(0);
  121. }
  122. main()   
  123. {
  124.         SHORT cylinder,head,sector;
  125.         
  126.         diskdata = &diskbuffer[0];
  127.                 /* point to first location in disk buffer */
  128.         diskport = CreatePort(0,0);
  129.         if(diskport == 0) exit(100);    /* error in createport */
  130.         diskreq = (struct IOExtTD *)CreateExtIO(diskport,
  131.                                                 sizeof(struct IOExtTD));
  132.                 /* make an io request block for communicating with the disk */
  133.         if(diskreq == 0) { DeletePort(diskport); exit(200); }
  134.  
  135.             error = OpenDevice(TD_NAME,0,diskreq,0);
  136.                     /* open the device for access, unit 0 is builtin drive */
  137.             printf("\nError value returned by OpenDevice was: %lx", error);
  138.     
  139.             /* now get the disk change value */
  140.             diskreq->iotd_Req.io_Command = TD_CHANGENUM;
  141.             DoIO(diskreq);
  142.             diskChangeCount = diskreq->iotd_Req.io_Actual;
  143.             printf("\nChange number for disk is currently %ld",diskChangeCount);    
  144.             MotorOn();
  145.             SeekFullRange(10);
  146.             for(cylinder=0; cylinder<80; cylinder++)    /* tracks to test */
  147.                { 
  148.                for(head=0; head<2; head++)      /* number of heads to test */
  149.                     for(sector=0; sector<11; sector++)  /* sectors to test */
  150.                     {
  151.                     ReadCylSec(cylinder, sector, head);
  152.                     if(diskreq->iotd_Req.io_Error != 0)
  153.                        printf("\nError At Cyl=%ld, Sc=%ld, Hd=%ld, Error=%ld",
  154.                                     cylinder,sector,head,
  155.                                     diskreq->iotd_Req.io_Error);
  156.                     }
  157.                 printf("\nCompleted reading Cylinder=%ld",cylinder);
  158.                 }
  159.             MotorOff();
  160.             CloseDevice(diskreq);
  161.  
  162.         DeleteExtIO(diskreq, sizeof(struct IOExtTD));
  163.         DeletePort(diskport);
  164. }       /* end of main */
  165.  
  166.  
  167. /***********************************************************************
  168. *
  169. *       Exec Support Function -- Extended IO Request
  170. *
  171. ***********************************************************************/
  172.  
  173. extern APTR AllocMem();
  174.  
  175. /****** exec_support/CreateExtIO **************************************
  176. *
  177. *   NAME
  178. *       CreateExtIO() -- create an Extended IO request
  179. *
  180. *   SYNOPSIS
  181. *       ioReq = CreateExtIO(ioReplyPort,size);
  182. *
  183. *   FUNCTION
  184. *       Allocates memory for and initializes a new IO request block
  185. *       of a user-specified number of bytes.
  186. *
  187. *   INPUTS
  188. *       ioReplyPort - a pointer to an already initialized
  189. *               message port to be used for this IO request's reply port.
  190. *
  191. *   RESULT
  192. *       Returns a pointer to the new block.  Pointer is of the type
  193. *       struct IORequest.
  194. *
  195. *       0 indicates inability to allocate enough memory for the request block
  196. *       or not enough signals available.
  197. *
  198. *   EXAMPLE
  199. *       struct IORequest *myBlock;
  200. *       if( (myBlock = CreateExtIO(myPort,sizeof(struct IOExtTD)) == NULL)
  201. *               exit(NO_MEM_OR_SIGNALS);
  202. *
  203. *       example used to allocate space for IOExtTD (trackdisk driver
  204. *       IO Request block for extended IO operations).
  205. *
  206. *   SEE ALSO
  207. *       DeleteExtIO
  208. *
  209. ***********************************************************************/
  210.  
  211. struct IORequest *CreateExtIO(ioReplyPort,size)
  212.     struct MsgPort *ioReplyPort;
  213.     LONG size;
  214. {
  215.     struct IORequest *ioReq;
  216.  
  217.     if (ioReplyPort == 0)
  218.         return ((struct IORequest   *) 0);
  219.  
  220.     ioReq = (struct IORequest *)AllocMem (size, MEMF_CLEAR | MEMF_PUBLIC);
  221.  
  222.     if (ioReq == 0)
  223.         return ((struct IORequest   *) 0);
  224.  
  225.     ioReq -> io_Message.mn_Node.ln_Type = NT_MESSAGE;
  226.     ioReq -> io_Message.mn_Node.ln_Pri = 0;
  227.  
  228.     ioReq -> io_Message.mn_ReplyPort = ioReplyPort;
  229.  
  230.     return (ioReq);
  231. }
  232.   
  233. /****** exec_support/DeleteExtIO **************************************
  234. *
  235. *   NAME
  236. *       DeleteExtIO() - return memory allocated for extended IO request
  237. *
  238. *   SYNOPSIS
  239. *       DeleteExtIO(ioReq,size);
  240. *
  241. *   FUNCTION
  242. *       See summary line at NAME.  Also frees the signal bit which
  243. *       had been allocated by the call to CreateExtIO.
  244. *
  245. *   INPUTS
  246. *       A pointer to the IORequest block whose resources are to be freed.
  247. *
  248. *   RESULT
  249. *       Frees the memory.  Returns (no error conditions shown)
  250. *
  251. *   EXAMPLE
  252. *       struct IORequest *myBlock;
  253. *       DeleteExtIO(myBlock,(sizeof(struct IOExtTD)));
  254. *
  255. *       example shows that CreateExtIO had been used to create a trackdisk
  256. *       (extended) IO Request block.
  257. *
  258. *   SEE ALSO
  259. *       CreateExtIO
  260. *
  261. **************************************************************************/
  262.   
  263. DeleteExtIO(ioExt,size)
  264.     struct IORequest *ioExt;
  265.     LONG size;
  266. {
  267.     ioExt -> io_Message.mn_Node.ln_Type = 0xff;
  268.     ioExt -> io_Device = (struct Device *) -1;
  269.     ioExt -> io_Unit = (struct Unit *) -1;
  270.  
  271.     FreeMem (ioExt, size);
  272. }
  273.